package com.aiyige.page.publish.uploadfile.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.db.table.UploadFile;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.widget.LabelProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/11/23.
 */

public class UploadFileAdapter extends BaseQuickAdapter<UploadFile, UploadFileAdapter.ViewHolder> {

    public UploadFileAdapter() {
        super(R.layout.upload_file_item, new LinkedList<UploadFile>());
    }

    @Override
    protected void convert(ViewHolder helper, UploadFile item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.statusMask)
        View statusMask;
        @BindView(R.id.statusIv)
        ImageView statusIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.statusTv)
        TextView statusTv;
        @BindView(R.id.labelProgressBar)
        LabelProgressBar labelProgressBar;
        @BindView(R.id.deleteBtn)
        ImageView deleteBtn;
        @BindView(R.id.trailTv)
        TextView trailTv;
        int currentUploadImgResId = 0;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.deleteBtn);
            addOnClickListener(R.id.statusMask);
        }

        public void bindData(UploadFile data) {
            titleTv.setText(data.getTitle());
            switch (data.getFileType()) {
                case PublishUtil.UPLOAD_FILE_TYPE_VIDEO:
                    GlideUtil.with(itemView.getContext())
                            .loadCover(data.getCoverUrl())
                            .into(coverIv);
                    break;
                case PublishUtil.UPLOAD_FILE_TYPE_PHOTO:
                    GlideUtil.with(itemView.getContext())
                            .loadCover(data.getFileLocalUrl())
                            .into(coverIv);
                    break;
            }
            if (data.getBussinessType() == PublishUtil.BUSINESS_TYPE_TRAIL) {
                deleteBtn.setVisibility(View.INVISIBLE);
                trailTv.setVisibility(View.VISIBLE);
            } else {
                deleteBtn.setVisibility(View.VISIBLE);
                trailTv.setVisibility(View.INVISIBLE);
            }

            if (data.getProgressStatus() == PublishUtil.PROGRESS_STATUS_UPLOAD_FINISH) {
                statusMask.setVisibility(View.INVISIBLE);
                statusIv.setVisibility(View.INVISIBLE);
                statusTv.setText(R.string.upload_finish);
                deleteBtn.setVisibility(View.INVISIBLE);
                labelProgressBar.setProgress(100);
            } else {
                statusMask.setVisibility(View.VISIBLE);
                statusIv.setVisibility(View.VISIBLE);
                switch (data.getControlOption()) {
                    case PublishUtil.CONTROL_OPTION_PAUSE:
                        if (currentUploadImgResId != R.drawable.suspend) {
                            statusIv.setImageResource(R.drawable.suspend);
                            currentUploadImgResId = R.drawable.suspend;
                        }
                        labelProgressBar.setProgress(100f * data.getUploadedSize() / data.getTotalSize());
                        statusTv.setText(R.string.pausing);
                        break;
                    case PublishUtil.CONTROL_OPTION_FAILED_PAUSE:
                        if (currentUploadImgResId != R.drawable.suspend) {
                            statusIv.setImageResource(R.drawable.suspend);
                            currentUploadImgResId = R.drawable.suspend;
                        }
                        labelProgressBar.setProgress(100f * data.getUploadedSize() / data.getTotalSize());
                        statusTv.setText(R.string.upload_failed_retry);
                        break;
                    case PublishUtil.CONTROL_OPTION_UPLOAD:
                        if (currentUploadImgResId != R.drawable.upload) {
                            statusIv.setImageResource(R.drawable.upload);
                            currentUploadImgResId = R.drawable.upload;
                        }
                        labelProgressBar.setProgress(100f * data.getUploadedSize() / data.getTotalSize());
                        switch (data.getProgressStatus()) {
                            case PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE:
                                statusTv.setText(String.format(itemView.getContext().getString(R.string.transcoding), data.getTranscodeProgress()));
                                break;
                            case PublishUtil.PROGRESS_STATUS_UPLOADING:
                                statusTv.setText(R.string.uploading_status);
                                break;
                        }
                        break;
                }
            }
        }
    }
}




